﻿<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="Shortcut Icon" href="../images/aecc.ico" type="image/x-icon" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="../StaticFiles/element-ui/element-ui.css">
  <link rel="stylesheet" href="../StaticFiles/nprogress/nprogress.min.css">
  <link rel="stylesheet" href="../styles/vue-page-common.css">
  <script src="../Scripts/vue.min.js"></script>
  <script src="../StaticFiles/element-ui/element-ui.js"></script>
  <script src="../Scripts/axios.min.js"></script>
  <script src="../StaticFiles/nprogress/nprogress.min.js"></script>
  <script src="../Scripts/dayjs.min.js"></script>
  <script src="../Scripts/axios.config.js"></script>
  <title>问题快反角色维护</title>
  <style>
    /* 穿梭框宽度设置 */
    .el-transfer-panel {
      width: 322px;
    }

    .el-dialog__body {
      padding-top: 10px;
      padding-bottom: 10px;
    }

    .el-cascader-menu__wrap {
      height: 300px !important;
    }

    .el-transfer-panel__body {
      height: 350px !important;
    }

    .el-transfer-panel__list {
      height: 300px !important;
    }
  </style>
</head>

<body>
  <div id="app" v-cloak v-on:keyup.enter="getData()">
    <!-- 报表标题 -->
    <vue2-report-header :title="'问题快反角色维护'"></vue2-report-header>
    <div style="padding: 10px;">
      <!-- 查询条件区 -->
      <el-form :inline="true" :model="queryInfo" ref="queryInfoRef" style="margin: 10px" :label-position="'left'">
        <el-form-item label="角色名称:">
          <el-input v-model.trim="queryInfo.rolename" placeholder="请输入角色名称" clearable></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="small" @click="getData" :disabled="!isLegel">查询
          </el-button>
          <el-button type="success" icon="el-icon-circle-plus-outline" size="small" @click="showAddRoleDialog"
            :disabled="!isLegel">添加
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 表格 -->
      <el-table v-loading="loading" v-loading.fullscreen.lock="loading" element-loading-text="数据加载中"
        element-loading-background="rgba(0,0,0,0.8)" :data="dataList" stripe border show-overflow-tooltip>
        <el-table-column type="index" width="40"></el-table-column>
        <el-table-column label="操作" width="190">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-circle-plus-outline" size="mini"
              @click="addRoleUsersDialogOpen(scope.row)">
              添加
            </el-button>
            <el-button type="danger" icon="el-icon-remove-outline" size="mini"
              @click="removeRoleUsersDialogOpen(scope.row)">
              移除
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="角色id" prop="roleid" width="120" v-if='false'></el-table-column>></el-table-column>
        <el-table-column label="角色名称" prop="rolename"></el-table-column>
        <el-table-column label="创建人" prop="createuser" width="100"></el-table-column>
        <el-table-column label="创建时间" prop="createdate" width="180" :formatter="replaceDate"></el-table-column>
        <el-table-column label="启用状态" prop="status" width="100">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" active-text="启用" inactive-text="" @change="changeStatus(scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="更新人" prop="updateuser" width="100"></el-table-column>
        <el-table-column label="更新时间" prop="updatedate" width="180" :formatter="replaceDate"></el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.currentPage" :page-sizes="[10,20,50]" :page-size="queryInfo.pageSize"
        layout="prev,pager,next,jumper,sizes,total" :total="total" :disabled="!isLegel">
      </el-pagination>

      <!-- 回到顶部 -->
      <el-backtop></el-backtop>

      <!-- 新增角色 弹出框 -->
      <el-dialog title="新增角色" :visible.sync="addRoleDialogVisible" width="50%" @close="addRoleDialogClose">
        <el-form :model="addRoleForm" :rules="addRoleFormRule" ref="addRoleFormRef" :inline="true" lable-width="100px"
          :label-position="'top'" status-icon>
          <el-form-item label="角色名称" prop="rolename">
            <el-input v-model="addRoleForm.rolename" style="width:680px;" placeholder="请输入角色名称"></el-input>
          </el-form-item>
        </el-form>
        <!-- 弹出窗底下按钮 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="addRoleDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addRoleBtnConfirm">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 角色添加用户 弹出框 -->
      <el-dialog title="角色添加用户" :visible.sync="addRoleUsersDialogVisible" width="60%" height="800"
        @close="addRoleUsersDialogClose">
        <el-form :model="addRoleUsersForm" ref="editFormRef" :inline="true" lable-width="100px">
          <el-form-item label="角色名称">
            <el-input v-model="addRoleUsersForm.rolename" disabled></el-input>
          </el-form-item>
          <el-form-item label="创建人">
            <el-input v-model="addRoleUsersForm.createuser" disabled></el-input>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-input v-model="addRoleUsersForm.createdate" disabled></el-input>
          </el-form-item>
          <el-form-item label="组织结构" prop="org">
            <el-cascader :options="orginazition" :props="{ checkStrictly: true}" ref="orginazitionRef"
              @change="selectOrgChanged" v-model="selectOrg" clearable style="width:757px;">
            </el-cascader>
          </el-form-item>
        </el-form>

        <!-- 穿梭框 -->
        <el-transfer filterable filter-placeholder="请输入人员" v-model="willBeAddedUsers" :data="userList"
          :titles="['未选', '已选']" :props="{key: 'userid',label: 'username'}" width="100%">
        </el-transfer>

        <!-- 弹出窗底下按钮 -->
        <span slot="footer" class="dialog-footer">
          <el-button @click="addRoleUsersDialogVisible = false" size="small">取 消</el-button>
          <el-button type="primary" @click="addRoleUsers" size="small" :disabled="addButtonDisabled">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 角色移除用户 弹出框 -->
      <el-dialog title="角色移除用户" :visible.sync="removeRoleUsersDialogVisible" width="60%"
        @close="removeRoleUsersDialogClose">
        <el-form :model="removeRoleUsersForm" ref="editFormRef" :inline="true" lable-width="100px">
          <el-form-item label="角色名称">
            <el-input v-model="removeRoleUsersForm.rolename" disabled></el-input>
          </el-form-item>
          <el-form-item label="创建人">
            <el-input v-model="removeRoleUsersForm.createuser" disabled></el-input>
          </el-form-item>
          <el-form-item label="创建时间">
            <el-input v-model="removeRoleUsersForm.createdate" disabled></el-input>
          </el-form-item>
        </el-form>
        <el-transfer filterable filter-placeholder="请输入人员" v-model="willBeRemovedUsers" :data="roleUsers"
          :titles="['未选', '已选']" :props="{key: 'userid',label: 'username'}" width="100%">
        </el-transfer>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false" size="small">取 消</el-button>
          <el-button type="primary" @click="removeRoleUsers" size="small" :disabled="deleteButtonDisabled">确 定
          </el-button>
        </span>
      </el-dialog>
    </div>
  </div>
  <script type="module">
    import { getParams, getUserWhiteList } from '../Scripts/hooks/useCommonMethod.js'
    import { replaceDate } from '../Scripts/hooks/format.js'
    import Vue2ReportHeader from '../Scripts/components/Vue2ReportHeader.js';
    const app = new Vue({
      el: '#app',
      components: { Vue2ReportHeader },
      data: {
        replaceDate,
        loading: false,
        total: 0,
        employeeid: '',
        employeename: '',
        queryInfo: {
          rolename: '',
          currentPage: 1,//当前页
          pageSize: 10//每页显示条数
        },
        isLegel: true,
        dataList: [],

        orginazition: [],//组织结构
        selectOrg: [],

        addRoleUsersDialogVisible: false,
        addRoleUsersForm: {},
        userList: [],// 用户列表
        willBeAddedUsers: [],// 将要被添加的用户

        removeRoleUsersDialogVisible: false,
        willBeRemovedUsers: [],// 将要被移除的用户
        removeRoleUsersForm: {},
        roleUsers: [],

        addButtonDisabled: false,//
        deleteButtonDisabled: false,

        columnIsHidden: true, //列是否隐藏

        addRoleDialogVisible: false,//添加对话框
        addRoleForm: {
          rolename: ''
        },
        addRoleFormRule: {
          rolename: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        },
        // 白名单
        whiteList: []
      },
      methods: {
        //查询数据
        async getData() {
          this.loading = true;
          const { data: { data, meta, total } } = await axios.post('../handler/report/BpmProblemRoleUsersMaint.ashx?action=getData', {
            type: 'getData',
            ...this.queryInfo,
          })
          this.loading = false;
          if (meta.status === 500) { return this.$message.error(meta.msg); }
          this.dataList = data.map(item => {
            return { ...item, status: item.status === 1 }; //将【启用状态】由字符串变更为布尔值
          })
          this.total = total;
        },
        // 添加角色用户对话框打开时
        async addRoleUsersDialogOpen(row) {
          if (this.whiteList.indexOf(this.employeeid) === -1) {
            return this.$message.error('该用户无权添加');
          }
          this.addRoleUsersDialogVisible = true;
          this.addRoleUsersForm = { ...row };
        },
        // 添加角色用户对话框关闭时
        addRoleUsersDialogClose() {
          this.addButtonDisabled = false;
          this.selectOrg = [];
          this.userList = [];
          this.willBeAddedUsers = [];
        },
        // 添加角色用户
        async addRoleUsers() {
          if (this.willBeAddedUsers.length === 0) {
            return this.$message.error('请从左侧至少选择1项到右侧');
          }
          // 防止重复点击
          this.addButtonDisabled = true;
          const { data: { data, meta, total } } = await axios.post('../handler/report/BpmProblemRoleUsersMaint.ashx?action=addRoleUsers', {
            userList: this.willBeAddedUsers,
            ...this.addRoleUsersForm,
            type: 'addRoleUsers'
          })
          if (meta.status == 200) {
            this.$message.success('添加成功!');
          } else {
            this.$message.error('添加失败!');
          }
          this.willBeAddedUsers = [];
          this.userList = [];
          this.selectOrg = [];
        },
        // 移除角色用户对话框打开时
        async removeRoleUsersDialogOpen(row) {
          if (this.whiteList.indexOf(this.employeeid) === -1) {
            return this.$message.error('该用户无权移除');
          }
          this.removeRoleUsersDialogVisible = true;
          this.removeRoleUsersForm = { ...row };

          const { data: { data, meta, total } } = await axios.post('../handler/report/BpmProblemRoleUsersMaint.ashx?action=getRoleUsers', {
            roleid: row.roleid,
            type: 'getRoleUsers'
          })

          if (meta.status !== 200) {
            return this.$message.error('数据请求失败');
          }

          this.roleUsers = data.map((item) => {
            return { userid: item.userid, username: item.username };
          })
        },
        // 移除角色用户对话框关闭时
        removeRoleUsersDialogClose() {
          this.deleteButtonDisabled = false;
        },
        // 移除角色用户
        async removeRoleUsers() {
          if (this.willBeRemovedUsers.length === 0) {
            return this.$message.error('请从左侧至少选择1项至右侧!');
          }

          // 更新启用状态前确认
          const confirmResult = await this.$confirm('即将移除已选择的用户，该操作不可撤销，请确认是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).catch(err => err)

          // 取消切换
          if (confirmResult !== 'confirm') {
            return this.$message.info('已取消移除用户');
          }

          // 防止重复点击
          this.deleteButtonDisabled = true;
          const { data: { data, meta, total } } = await axios.post('../handler/report/BpmProblemRoleUsersMaint.ashx?action=removeRoleUsers', {
            userList: this.willBeRemovedUsers,
            roleid: this.removeRoleUsersForm.roleid,
            type: 'removeRoleUsers'
          })

          if (meta.status == 200) {
            this.$message.success(meta.msg);
          } else {
            this.$message.error(meta.msg);
          }

          this.willBeRemovedUsers = [];
          this.removeRoleUsersDialogVisible = false;
          return
        },
        handleCurrentChange(newPage) {
          this.queryInfo.currentPage = newPage;
          this.getData();
        },
        handleSizeChange(newSize) {
          this.queryInfo.pageSize = newSize;
          this.getData();
        },
        // 添加角色对话框打开时
        async showAddRoleDialog() {
          const { data: superAdmin } = await axios.post('../handler/report/CommonActionHandle.ashx?action=isSuperAdmin', {
            type: 'isSuperAdmin',
            employeeid: getParams("uid")
          });
          if (superAdmin.total == 0) {
            return this.$message.error('该用户无权添加角色');
          }
          this.addRoleDialogVisible = true;
        },
        //添加角色对话框关闭时
        addRoleDialogClose() {
          this.$refs["addRoleFormRef"].resetFields();
        },
        // 确认添加角色
        addRoleBtnConfirm() {
          this.$refs["addRoleFormRef"].validate((valid) => {
            if (valid) {
              let rolename = this.addRoleForm.rolename.trim();
              if (rolename === '') {
                return this.$message.error('角色名称不能为空');
              }
              // 新增
              this.addRole(rolename);
            }
          })
        },
        // 新增角色名称
        async addRole(rolename) {
          const { data: { data, meta, total } } = await axios.post('../handler/report/BpmProblemRoleUsersMaint.ashx?action=addRole', {
            type: 'addRole',
            rolename,
            employeeid: this.employeeid
          })
          if (meta.status === 200 && total > 0) {
            this.$message.success(meta.msg);
          } else {
            this.$message.error(meta.msg);
          }
        },
        // 切换启用状态
        async changeStatus(row) {
          if (this.whiteList.indexOf(this.employeeid) === -1) {
            row.status = !row.status;
            return this.$message.error('该用户无权更改状态');
          }
          // 更新启用状态前确认
          const confirmResult = await this.$confirm('是否切换启用状态?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).catch(err => err)

          // 取消切换
          if (confirmResult !== 'confirm') {
            row.status = !row.status;
            return this.$message.info('已取消切换!');
          }

          const { data: { data, meta, total } } = await axios.post('../handler/report/BpmProblemRoleUsersMaint.ashx?action=changeStatus', {
            type: 'changeStatus',
            roleid: row.roleid,
            employeeid: this.employeeid,
            status: row.status
          });

          if (meta.status === 500) {
            return this.$message.error(meta.msg);
          }

          if (total > 0) {
            this.$message.success('状态切换成功');
            this.getData();
          } else {
            return this.$message.error('状态切换失败');
          }
        },
        // 查询当前用户信息
        async getEmployeInfo(employeeid) {
          const { data: { data, meta, total } } = await axios({
            method: 'POST',
            url: '../handler/report/CommonActionHandle.ashx?action=getEmployeInfo',
            data: {
              type: 'getEmployeInfo',
              employeeid
            }
          })
          if (data.length > 0) {
            this.employeename = data[0].employeename;
          }
        },
        // 获取组织结构
        async getOrginazition() {
          const { data: { data, meta, total } } = await axios.post('../handler/report/FeedbackTypeMaint.ashx?action=getOrginazition', {
            type: 'getOrginazition'
          })
          this.orginazition = data;
        },
        // 角色添加用户所选组织结构变更
        async selectOrgChanged(val) {
          this.userList = [];
          this.willBeAddedUsers = [];
          if (!val.length) {
            return;
          }
          const { data: { data, meta, total } } = await axios.post('../handler/report/BpmProblemRoleUsersMaint.ashx?action=getUserList', {
            roleid: this.addRoleUsersForm.roleid,
            departmentid: this.selectOrg,
            type: 'getUserList'
          })

          if (meta.status !== 200) {
            return this.$message.error('数据请求失败');
          }
          this.userList = data.map((item) => {
            return {
              userid: item.userid,
              username: item.username,
              disabled: item.disabled === 'true'
            }
          })
        }
      },
      async created() {
        this.employeeid = getParams("uid");
        if (this.employeeid == "") {
          this.isLegel = false;
          return this.$notify.error('该用户无权使用该功能');
        }
        this.getData();
        this.getOrginazition();
        // 获取用户权限白名单
        const pageurl = window.location.pathname.split('/')[3];
        this.whiteList = await getUserWhiteList(pageurl, this.$message);
      },
      watch: {
        queryInfo: {
          handler() {
            this.queryInfo.currentPage = 1
          },
          deep: true
        }
      }
    })
  </script>
</body>

</html>